package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.dao.ContractMaterialDao;
import com.pgytesting.secondhandcar.dao.OrderNameDao;
import com.pgytesting.secondhandcar.model.ContractMaterial;
import com.pgytesting.secondhandcar.model.OrderName;
import com.pgytesting.secondhandcar.model.ReviewInformation;
import com.pgytesting.secondhandcar.service.ContractMaterialService;
import com.pgytesting.secondhandcar.service.ReviewInformationService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;
import com.pgytesting.secondhandcar.vo.Parameters;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 合同材料(ContractMaterial)表服务实现类
 *
 * @author makejava
 * @since 2019-08-12 18:55:58
 */
@Service("contractMaterialService")
public class ContractMaterialServiceImpl implements ContractMaterialService {
    @Resource
    private ContractMaterialDao contractMaterialDao;
    @Autowired
    private ReviewInformationService reviewInformationService;
    @Autowired
    private OrderNameDao orderNameDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ContractMaterial queryById(String id) {
        return this.contractMaterialDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ContractMaterial> queryAllByLimit(int offset, int limit) {
        return this.contractMaterialDao.queryAllByLimit(offset, limit);
    }
    
    /**
     * 条件查询多条数据
     *
     * @param contractMaterial 实例对象
     * @return 对象列表
     */
    @Override
    public List<ContractMaterial> queryAll(ContractMaterial contractMaterial) {
        return this.contractMaterialDao.queryAll(contractMaterial);
    }

    /**
     * 新增数据
     *
     * @param contractMaterial 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ContractMaterial insert(Map<String,Object> paraMap) {
    	
    	ContractMaterial contractMaterial = new ContractMaterial();
    	contractMaterial.setId(UtilMethod.getUniqueCode());
    	contractMaterial.setContractMaterial(paraMap.get("contractMaterial").toString());
    	contractMaterial.setCreatedBy(SessionUtil.getSessionMcpCar().getUserNumber());
    	contractMaterial.setCreatedTime(UtilMethod.unixLong());
    	contractMaterial.setStatus(0);
        this.contractMaterialDao.insert(contractMaterial);
        
        OrderName on = orderNameDao.queryById(paraMap.get("id").toString());
        ReviewInformation ri = new ReviewInformation();
        ri.setId(on.getReviewInformationId());
        ri.setContractMaterialId(contractMaterial.getId());
        ri.setUpdatedBy(SessionUtil.getSessionMcpCar().getUserNumber());
        ri.setApprovalStatus(Parameters.ORDER_26002.toString());
        reviewInformationService.update(ri);
        
        return contractMaterial;
    }

    /**
     * 修改数据
     *
     * @param contractMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public ContractMaterial update(ContractMaterial contractMaterial) {
        this.contractMaterialDao.update(contractMaterial);
        return this.queryById(contractMaterial.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.contractMaterialDao.deleteById(id) > 0;
    }
}